#!/usr/bin/python
# encoding:utf-8

# ****************************************************************
# Description： 测试工具GUI界面
# Author： jf
# Date：20170625
# version：
# ****************************************************************

from Tkinter import *
from ATcmdtest.mod_config import *
from main import *
import multiprocessing


def AutoTest_GUI():
	master = Tk()
	master.title('AutoTest Tool    V1.0')

	# 使用labelframe代替
	# lable1 = "请输入需要测试的sheet页名称，全部测试选择ALL"
	# Label(master, text=lable1).grid(row=0, columnspan=4, sticky=W, padx=20)

	groupsheet = LabelFrame(master, text = "请输入需要测试的sheet页名称，全部测试选择ALL", padx = 5, pady = 5)
	groupsheet.grid(padx = 20, pady = 20)

	# 设置选择测试sheet页面控件
	# sheetnames = [('ALL', 1), ('basic', 2), ('phonebook', 3), ('message', 4),
	# 			  ('network', 5), ('phone', 6), ('safe', 7), ('TCPIP', 8), ('wifi', 9)]

	sheetnames = ['ALL', 'basic', 'phonebook', 'message', 'network', 'call', 'safe', 'TCPIP', 'wifi']

	def cbButtonAll():
		if varAll.get() == TRUE:
			varbasic.set(TRUE)
			varmessage.set(TRUE)
			varphonebook.set(TRUE)
			varmessage.set(TRUE)
			varnetwork.set(TRUE)
			varcall.set(TRUE)
			varsafe.set(TRUE)
			vartcpip.set(TRUE)
			varwifi.set(TRUE)
			# print getConfig("CFG", "SheetName")
			setConfigSheetname("CFG", "sheetname", "ALL")
		# print getConfig("CFG", "SheetName")
		else:
			varbasic.set(FALSE)
			varmessage.set(FALSE)
			varphonebook.set(FALSE)
			varmessage.set(FALSE)
			varnetwork.set(FALSE)
			varcall.set(FALSE)
			varsafe.set(FALSE)
			vartcpip.set(FALSE)
			varwifi.set(FALSE)
			setConfigSheetname("CFG", "sheetname", "NONE")
		# print 2

	varAll = BooleanVar()
	checkall = Checkbutton(groupsheet, text = sheetnames[0], variable = varAll, command = cbButtonAll)
	checkall.grid(row = 0, column = 0, sticky = W, padx = 20)

	def cbButtonBasic():
		ret = "basic"
		if varbasic.get() == TRUE:
			# print 3
			setConfigSheetname("CFG", "sheetname", ret)
		else:
			# print 4
			setConfigSheetnameDel("CFG", "sheetname", ret)
			varAll.set(FALSE)

	varbasic = BooleanVar()
	checkbasic = Checkbutton(groupsheet, text = sheetnames[1], variable = varbasic, command = cbButtonBasic)
	checkbasic.grid(row = 0, column = 1, sticky = W, padx = 20)

	def cbButtonPhonebook():
		ret = "Phonebook"
		if varphonebook.get() == TRUE:
			# print 5
			setConfigSheetname("CFG", "sheetname", ret)
		else:
			# print 6
			setConfigSheetnameDel("CFG", "sheetname", ret)
			varAll.set(FALSE)

	varphonebook = BooleanVar()
	checkphonebook = Checkbutton(groupsheet, text = sheetnames[2], variable = varphonebook, command =
	cbButtonPhonebook)
	checkphonebook.grid(row = 0, column = 2, sticky = W, padx = 20)

	def cbButtonMessage():
		ret = "message"
		if varmessage.get() == TRUE:
			# print 7
			setConfigSheetname("CFG", "sheetname", ret)
		else:
			# print 8
			setConfigSheetnameDel("CFG", "sheetname", ret)
			varAll.set(FALSE)

	varmessage = BooleanVar()
	checkmessage = Checkbutton(groupsheet, text = sheetnames[3], variable = varmessage, command = cbButtonMessage)
	checkmessage.grid(row = 0, column = 3, sticky = W, padx = 20)

	def cbButtonNetwork():
		ret = "network"
		if varnetwork.get() == TRUE:
			# print 9
			setConfigSheetname("CFG", "sheetname", ret)
		else:
			# print 10
			setConfigSheetnameDel("CFG", "sheetname", ret)
			varAll.set(FALSE)

	varnetwork = BooleanVar()
	checknetwork = Checkbutton(groupsheet, text = sheetnames[4], variable = varnetwork, command = cbButtonNetwork)
	checknetwork.grid(row = 1, column = 0, sticky = W, padx = 20)

	def cbcall():
		ret = "call"
		if varcall.get() == TRUE:
			# print 11
			setConfigSheetname("CFG", "sheetname", ret)
		else:
			# print 12
			setConfigSheetnameDel("CFG", "sheetname", ret)
			varAll.set(FALSE)

	varcall = BooleanVar()
	checkcall = Checkbutton(groupsheet, text = sheetnames[5], variable = varcall, command = cbcall)
	checkcall.grid(row = 1, column = 1, sticky = W, padx = 20)

	def cbButtonSafe():
		ret = "safe"
		if varsafe.get() == TRUE:
			# print 13
			setConfigSheetname("CFG", "sheetname", ret)
		else:
			# print 14
			setConfigSheetnameDel("CFG", "sheetname", ret)
			varAll.set(FALSE)

	varsafe = BooleanVar()
	checksafe = Checkbutton(groupsheet, text = sheetnames[6], variable = varsafe, command = cbButtonSafe)
	checksafe.grid(row = 1, column = 2, sticky = W, padx = 20)

	def cbButtonTcpip():
		ret = "tcpip"
		if vartcpip.get() == TRUE:
			# print 15
			setConfigSheetname("CFG", "sheetname", ret)
		else:
			# print 16
			setConfigSheetnameDel("CFG", "sheetname", ret)
			varAll.set(FALSE)

	vartcpip = BooleanVar()
	checkTcpip = Checkbutton(groupsheet, text = sheetnames[7], variable = vartcpip, command = cbButtonTcpip)
	checkTcpip.grid(row = 1, column = 3, sticky = W, padx = 20)

	def cbButtonWifi():
		ret = "wifi"
		if varwifi.get() == TRUE:
			# print 17
			setConfigSheetname("CFG", "sheetname", ret)
		else:
			# print 18
			setConfigSheetnameDel("CFG", "sheetname", ret)
			varAll.set(FALSE)

	varwifi = BooleanVar()
	checkwifi = Checkbutton(groupsheet, text = sheetnames[8], variable = varwifi, command = cbButtonWifi)
	checkwifi.grid(row = 2, column = 0, sticky = W, padx = 20)

	# var = []
	#
	# # for sheetname, num in sheetnames:
	# for i in range(len(sheetnames)):
	# 	var.append(IntVar())
	# 	checkbutton = Checkbutton(groupsheet,
	# 							  text=sheetnames[i],
	# 							  variable=var[-1],
	# 							  # onvalue=sheetname,
	# 							  # offvalue=None
	# 							  command = cbbuttoncheck(i)
	# 							  )
	#
	# 	checkbutton.grid(row=((i / 4) + 1), column=(i % 4), sticky=W, padx=20)



	# 设置界面图标
	photo = PhotoImage(file = '2qq.gif')
	label = Label(image = photo)
	label.image = photo
	label.grid(row = 0, column = 4, rowspan = 4, sticky = W + E + N + S, padx = 5, pady = 5)

	# 设置开始测试控件
	# START 按键的calback函数
	def startCallBack():
		p = multiprocessing.Process(target = case_begin)
		# p = threading.Thread(target = case_begin)
		p.daemon = True
		p.start()
		state = getConfig("CFG", "casestate")
		if state != 'True':
			sleep(0.5)
			setConfig("CFG", "casestate", True)  # 运行状态
			txt.insert(INSERT, "开始测试 ...\n")
		else:
			txt.insert(INSERT, "正在测试，请稍后 ...\n")

	buttonStart = Button(master, text = "START",
						 width = 15,
						 height = 3,
						 command = startCallBack,
						 state = ACTIVE)
	buttonStart.grid(row = 4, rowspan = 2, column = 4)

	txt = Text(master,
			   width = 69,
			   height = 5,
			   wrap = WORD)
	txt.grid(row = 5, column = 0, columnspan = 4, sticky = W, padx = 20, pady = 10)


def cfg_init():
	ret = 'NONE'
	setConfigSheetname("CFG", "sheetname", ret)
	setConfig("CFG", "casestate", ret)


# mainloop()

if __name__ == "__main__":
	AutoTest_GUI()
	cfg_init()
	mainloop()
